package com.tencent.sr.iris.activity.service.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.tencent.sr.iris.activity.dependency.dto.data.CommissionInfoDTO;
import com.tencent.sr.iris.activity.interfaces.message.DistributionOrderEvent;
import com.tencent.sr.iris.activity.interfaces.request.acticity.AwardConfirmRequest;
import com.tencent.sr.iris.activity.interfaces.response.group.task.QuFaCheckHasJoinSpecialPromoteRes;
import com.tencent.sr.iris.user.core.service.interfaces.response.activity.SpecialInviteResponse;
import com.tencent.sr.tech.dynamic.config.spring.annotation.DynamicConfigObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 接口返回值mock，模拟测试场景使用
 *
 * @author xiaojun
 */
@Data
@Component
@DynamicConfigObject(key = "responseMockConfig")
@Slf4j
public class ResponseMockConfig {


    /**
     * 统计佣金接口mock, key:uid，value:佣金接口返回值
     */
    private Map<String, CommissionInfoDTO> commissionResultMock;
    /**
     * 统计佣金接口mock, key:uid，value:佣金接口返回值
     */
    private Map<String, DistributionOrderEvent> distributionOrderEventMock;
    /**
     * 成为见习会员时间接口mock, key:uid，value:成为见习会员时间 格式：yyyy-MM-dd HH:mm:ss
     */
    private Map<String, String> traineeTimeResultMock;

    /**
     * 邀请有礼逻辑变化时间接口mock, key:uid，value:逻辑切换时间 格式：yyyy-MM-dd HH:mm:ss
     */
    private Map<String, String> inviteChangeTimeMock;

    /**
     * 邀请有礼上限逻辑变化时间接口mock, key:uid，value:逻辑切换时间 格式：yyyy-MM-dd HH:mm:ss
     */
    private Map<String, String> inviteUpperLimitChangeTimeMock;

    /**
     * 成为当前等级的时间接口mock, key:uid，value:成为当前等级用户的时间 格式：yyyy-MM-dd HH:mm:ss
     */
    private Map<String, String> userMemberTimeResultMock;

    /**
     * 查询特邀信息mock, key:uid，value:特邀信息
     */
    private Map<String, SpecialInviteResponse> specialInviteMock;

    /**
     * 查询特邀老团推团mock, key:uid，value:特邀信息
     */
    private Map<String, QuFaCheckHasJoinSpecialPromoteRes> specialOldTttMock;

    /**
     * 等级维度邀请限额，key为等级id
     */
    private Map<String, Integer> levelInviteLimitMock = Maps.newHashMap();

    /**
     * 是否启用新上级等级准入（依赖用户刷数。需刷完数再切换新准入规则）
     *
     * @return
     */
    private boolean useNewPidLevel = false;

    /**
     * 活动的普通用户开始参与时间
     */
    private String registGiftEndTime = "2023-09-01 00:00:00";

    /**
     * 特邀结束时间
     */
    private String specialInviteEndTime = "2023-09-01 00:00:00";

    /**
     * 需要mock分享数的月份，格式：2023-09
     */
    private List<String> visitMockMonth = Arrays.asList("2023-08", "2023-09");

    /**
     * 需要mock分享数的月份，格式：2023-09
     */
    private Integer mockVisitCount = 1;


    /**
     * 发奖确认参数mock，key为uid
     */
    private Map<String, AwardConfirmRequest> awardConfirmMock = Maps.newHashMap();

    /**
     * 首单发奖切换时间
     */
    private String firstOrderAwardChangeDate = "2023-10-31 23:59:59";

    /**
     * 获取uid的mock数据
     *
     * @param uid
     * @return
     */
    public CommissionInfoDTO getMockCommission(String uid) {
        CommissionInfoDTO dto = null;
        try {
            if (CollectionUtil.isEmpty(commissionResultMock)) {
                return null;
            }
            dto = commissionResultMock.get(uid);
        } catch (Exception e) {
            log.error("getMockCommission error, uid:{}", uid, e);
        }
        return dto;
    }

    /**
     * 获取uid的mock数据
     *
     * @param uid
     * @return
     */
    public DistributionOrderEvent getMockDistributionOrderEvent(String uid) {
        DistributionOrderEvent dto = null;
        try {
            if (CollectionUtil.isEmpty(distributionOrderEventMock) || StringUtils.isBlank(uid)) {
                return null;
            }
            dto = distributionOrderEventMock.get(uid);
        } catch (Exception e) {
            log.error("distributionOrderEventMock error, uid:{}", uid, e);
        }
        return dto;
    }

    /**
     * 获取uid的mock数据
     *
     * @param uid
     * @return
     */
    public Long getMockTraineeTime(String uid) {

        Long traineeTime = null;
        try {
            if (CollectionUtil.isEmpty(traineeTimeResultMock)) {
                return null;
            }
            String timeStr = traineeTimeResultMock.get(uid);
            if (Strings.isNullOrEmpty(timeStr)) {
                return null;
            }
            traineeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr).getTime();
        } catch (Exception e) {
            log.error("getMockTraineeTime error uid:{}", uid, e);
        }
        return traineeTime;
    }

    /**
     * 获取uid的mock数据
     *
     * @param uid
     * @return
     */
    public Long getInviteChangeTime(String uid) {

        Long changeTime = null;
        try {
            if (CollectionUtil.isEmpty(inviteChangeTimeMock)) {
                return null;
            }
            String timeStr = inviteChangeTimeMock.get(uid);
            if (Strings.isNullOrEmpty(timeStr)) {
                return null;
            }
            changeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr).getTime();
        } catch (Exception e) {
            log.error("getInviteChangeTime error uid:{}", uid, e);
        }
        return changeTime;
    }

    /**
     * 获取uid的mock数据
     * @param uid
     * @return
     */
    public Long getInviteUpperLimitChangeTime(String uid) {

        Long changeTime = null;
        try {
            if(CollectionUtil.isEmpty(inviteUpperLimitChangeTimeMock)){
                return null;
            }
            String timeStr = inviteUpperLimitChangeTimeMock.get(uid);
            if(Strings.isNullOrEmpty(timeStr)){
                return null;
            }
            changeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr).getTime();
        } catch (Exception e) {
            log.error("getInviteUpperLimitChangeTime error uid:{}", uid, e);
        }
        return changeTime;
    }

    /**
     * 获取uid成为当前等级的时间
     *
     * @param uid
     * @return
     */
    public Long getMockUserMemberTime(String uid) {

        Long traineeTime = null;
        try {
            if (CollectionUtil.isEmpty(userMemberTimeResultMock)) {
                return null;
            }
            String timeStr = userMemberTimeResultMock.get(uid);
            if (Strings.isNullOrEmpty(timeStr)) {
                return null;
            }
            traineeTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeStr).getTime();
        } catch (Exception e) {
            log.error("getMockUserMemberTime error uid:{}", uid, e);
        }
        return traineeTime;
    }

    /**
     * 获取uid的特邀信息
     *
     * @param uid
     * @return
     */
    public SpecialInviteResponse getSpecialInviteMock(String uid) {

        try {
            if (CollectionUtil.isEmpty(specialInviteMock)) {
                return null;
            }
            return specialInviteMock.get(uid);
        } catch (Exception e) {
            log.error("getSpecialInviteMock error uid:{}", uid, e);
        }
        return null;
    }

    /**
     * 获取uid的特邀老团推团信息
     *
     * @param uid
     * @return
     */
    public QuFaCheckHasJoinSpecialPromoteRes getSpecialOldTttMock(String uid) {

        try {
            if (CollectionUtil.isEmpty(specialOldTttMock)) {
                return null;
            }
            return specialOldTttMock.get(uid);
        } catch (Exception e) {
            log.error("getSpecialOldTttMock error uid:{}", uid, e);
        }
        return null;
    }

    /**
     * 是否需要mock
     *
     * @return
     */
    public boolean needMock(Date date) {
        try {
            if (CollectionUtil.isEmpty(visitMockMonth) || date == null) {
                return false;
            }
            String format = DateUtil.format(date, "yyyy-MM");
            return visitMockMonth.contains(format);
        } catch (Exception e) {
            log.error("needMock error date:{}", date);
        }
        return false;
    }

}
